package cn.com.domain.vo;

import cn.com.Util.StringUtil;
import cn.com.domain.BaseEntity;
import lombok.Data;

import java.util.Date;

/**
 * @Author CRL
 * @Date 2021/4/11 23:17
 */
@Data
public class WorkDiaryVo extends BaseEntity {

    /**
     * 工作日期
     */
    private Date workDate;

    /**
     * 工作内容
     */
    private String workContent;

    /**
     * 是否完成
     */
    private Boolean isAchieve;

    /**
     * 操作
     */
    private String operation;

    public String hash;
    public String previousHash;
    private String data;
    private long timeStamp;
    private int nonce;

    public WorkDiaryVo() {
    }

    //加密公文模块
    public WorkDiaryVo(String data, String previousHash) {
        this.data = data;
        this.previousHash = previousHash;
        this.timeStamp = System.currentTimeMillis();
        this.hash = calculateHash(); //Making sure we do this after we set the other values.
    }

    //Calculate new hash based on blocks contents
    //加密Hash值
    public String calculateHash() {
        return StringUtil.applySha256(
                previousHash +
                        Long.toString(timeStamp) +
                        Integer.toString(nonce) +
                        data
        );
    }

    //Increases nonce value until hash target is reached.
    public void mineBlock(int difficulty) {
        String target = StringUtil.getDificultyString(difficulty); //Create a string with difficulty * "0"
        while (!hash.substring(0, difficulty).equals(target)) {
            nonce++;
            hash = calculateHash();
        }
        System.out.println("Block Mined!!! : " + hash);
    }


}
